package com.ztgf.shopping.service.epg;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.commodity.models.service.ICommodityInfoService;
import com.ztgf.commodity.models.vo.CommodityPriceVO;
import com.ztgf.shopping.common.config.YmlConfig;
import com.ztgf.shopping.mapper.epg.EpgShowWindowMapper;
import com.ztgf.shopping.models.bo.epg.EpgShowWindowBO;
import com.ztgf.shopping.models.bo.epg.EpgShowWindowSaveBO;
import com.ztgf.shopping.models.constant.ShoppingConstant;
import com.ztgf.shopping.models.dto.epg.EpgShowWindowDTO;
import com.ztgf.shopping.models.enums.ShoppingCodeEnum;
import com.ztgf.shopping.models.service.epg.IEpgShowWindowService;
import com.ztgf.shopping.models.vo.epg.EpgShowWindowVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * author: chenyu.wang
 * date: 2020-05-19 11:33
 * Description: 橱窗表业务层
 */
@DubboService
public class EpgShowWindowServiceImpl extends ServiceImpl<EpgShowWindowMapper, EpgShowWindowDTO> implements IEpgShowWindowService {

    @Resource
    private YmlConfig ymlConfig;

    @Autowired
    private EpgShowWindowMapper epgShowWindowMapper;

    @DubboReference
    private ICommodityInfoService iCommodityInfoService;

    @Override
    public boolean add(EpgShowWindowSaveBO showWindowSaveBO) {
        EpgShowWindowDTO epgShowWindowDTO = new EpgShowWindowDTO();
        BeanUtils.copyProperties(showWindowSaveBO, epgShowWindowDTO);
        epgShowWindowDTO.setId(ymlConfig.getPkId());
        return this.insert(epgShowWindowDTO);
    }

    /**
     * 删除功能功能相关的店铺橱窗
     * @return
     */
    public boolean deleteByModuleId(Long moduleId){
        Map<String, Object> columnMap = new HashMap<>();
        columnMap.put("module_id",moduleId);
        return epgShowWindowMapper.deleteByMap(columnMap) > 0;
    }

    /**
     * 根据条件查询橱窗信息
     * @param epgShowWindowBO
     * @return
     */
    @Override
    public Page<EpgShowWindowVO> getBySelectShowWindowInfoPage(EpgShowWindowBO epgShowWindowBO) {
        Wrapper<EpgShowWindowDTO> wrapper = getBySelectShowWindowInfo(epgShowWindowBO);
        Page epgShowWindowDTOListPage = selectPage(new Page(epgShowWindowBO.getPageNo(), epgShowWindowBO.getPageSize()), wrapper);

        List<EpgShowWindowVO> epgShowWindowVOList = new ArrayList<>();
        for (Object epgShowWindowDTOTemp : epgShowWindowDTOListPage.getRecords()) {
            EpgShowWindowVO epgShowWindowVO = new EpgShowWindowVO();
            BeanUtils.copyProperties(epgShowWindowDTOTemp, epgShowWindowVO);
            CommodityPriceVO commodityPriceVO = iCommodityInfoService.getByIdOtherInterface(epgShowWindowVO.getCommodityId());
            epgShowWindowVO.setCommodityPriceVO(commodityPriceVO);
            epgShowWindowVOList.add(epgShowWindowVO);
        }
        return epgShowWindowDTOListPage.setRecords(epgShowWindowVOList);
    }

    /**
     * 根据条件查询橱窗信息 无页码
     * @param epgShowWindowBO
     * @return
     */
    @Override
    public List<EpgShowWindowVO> getBySelectShowWindowInfoNoPage(EpgShowWindowBO epgShowWindowBO) {
        epgShowWindowBO = addMustData(epgShowWindowBO);
        Wrapper<EpgShowWindowDTO> wrapper = getBySelectShowWindowInfo(epgShowWindowBO);
        List<EpgShowWindowDTO> epgShowWindowDTOList = null;
        if(epgShowWindowBO.getPageNo() == 0 && epgShowWindowBO.getPageSize() == 0)
            epgShowWindowDTOList =  epgShowWindowMapper.selectPage( new Page<>(0, ShoppingConstant.SHOW_WINDOW),wrapper);
        else{
            epgShowWindowDTOList =  selectList(wrapper);
        }

        List<EpgShowWindowVO> epgShowWindowVOList = new ArrayList<>();
        for (EpgShowWindowDTO epgShowWindowDTOTemp : epgShowWindowDTOList) {
            EpgShowWindowVO epgShowWindowVO = new EpgShowWindowVO();
            BeanUtils.copyProperties(epgShowWindowDTOTemp, epgShowWindowVO);
            CommodityPriceVO commodityPriceVO = iCommodityInfoService.getByIdOtherInterface(epgShowWindowVO.getCommodityId());
            epgShowWindowVO.setCommodityPriceVO(commodityPriceVO);
            epgShowWindowVOList.add(epgShowWindowVO);
        }
        return epgShowWindowVOList;
    }

    /**
     * 添加必要数据
     * @param epgShowWindowBO
     * @return
     */
    private EpgShowWindowBO addMustData(EpgShowWindowBO epgShowWindowBO){
        epgShowWindowBO.setIsPublish(ShoppingCodeEnum.PUBLIC_1.getCode());
        epgShowWindowBO.setStatus(ShoppingCodeEnum.STATUS_0.getCode());
        epgShowWindowBO.setSort(ShoppingCodeEnum.SORT_ASC.getCode());
        return epgShowWindowBO;
    }

    private  Wrapper<EpgShowWindowDTO> getBySelectShowWindowInfo(EpgShowWindowBO epgShowWindowBO) {
        Wrapper<EpgShowWindowDTO> wrapper = new EntityWrapper<>();
        EpgShowWindowDTO epgShowWindowDTO = new EpgShowWindowDTO();
        BeanUtils.copyProperties(epgShowWindowBO, epgShowWindowDTO);
        wrapper.eq("status", epgShowWindowBO.getStatus());
        wrapper.eq("is_publish", epgShowWindowBO.getIsPublish());
        wrapper.eq("module_id", epgShowWindowBO.getModuleId());
        if(ShoppingCodeEnum.SORT_DESC.getCode() == epgShowWindowBO.getSort())
            wrapper.orderBy("sort", false);
        else
            wrapper.orderBy("sort", true);
        return wrapper;
    }
}
